config->tabWidth !== 0 ? $phpcsFile->config->tabWidth : self::DEFAULT_INDENTATION_WIDTH); } /** * @param list $codePointers */ public static function removeIndentation(File $phpcsFile, array $codePointers, string $defaultIndentation): string { $tokens = $phpcsFile->getTokens(); $eolLength = strlen($phpcsFile->eolChar); $code = ''; $inHeredoc = false; $indentation = self::getOneIndentationLevel($phpcsFile); $indentationLength = strlen($indentation); foreach ($codePointers as $no => $codePointer) { $content = $tokens[$codePointer]['content']; if ( !$inHeredoc && ( $no === 0 || substr($tokens[$codePointer - 1]['content'], -$eolLength) === $phpcsFile->eolChar ) ) { if ($content === $phpcsFile->eolChar) { // Nothing } elseif (substr($content, 0, $indentationLength) === $indentation) { $content = substr($content, $indentationLength); } else { $content = $defaultIndentation . ltrim($content); } } if (in_array($tokens[$codePointer]['code'], [T_START_HEREDOC, T_START_NOWDOC], true)) { $inHeredoc = true; } elseif (in_array($tokens[$codePointer]['code'], [T_END_HEREDOC, T_END_NOWDOC], true)) { $inHeredoc = false; } $code .= $content; } return rtrim($code); } public static function convertSpacesToTabs(File $phpcsFile, string $code): string { // @codeCoverageIgnoreStart if ($phpcsFile->config->tabWidth === 0) { return $code; } // @codeCoverageIgnoreEnd return preg_replace_callback('~^([ ]+)~m', static function (array $matches) use ($phpcsFile): string { $tabsCount = (int) floor(strlen($matches[1]) / $phpcsFile->config->tabWidth); $spacesCountToRemove = $tabsCount * $phpcsFile->config->tabWidth; return str_repeat("\t", $tabsCount) . substr($matches[1], $spacesCountToRemove); }, $code); } }